package cn.tedu.repository.impl;

import cn.tedu.bo.WorkerBO;
import cn.tedu.dao.WorkerDao;
import cn.tedu.param.WorkerCreateParam;
import cn.tedu.repository.WorkerRepository;
import cn.tedu.repository.cache.impl.RedisCacheEntry;
import cn.tedu.repository.converter.WorkerConverter;
import com.tarena.passport.protocol.LoginUser;
import com.tarena.passport.sdk.context.SecurityContext;
import com.tarena.tp.luban.worker.po.Worker;
import com.tarena.tp.luban.worker.server.common.enums.AccountStatusEnum;
import com.tarena.tp.luban.worker.server.common.enums.AuditStatusEnum;
import com.tarena.tp.luban.worker.server.common.enums.StatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

@Repository
public class WorkerRepositoryImpl extends RedisCacheEntry<WorkerBO> implements WorkerRepository {



    @Autowired
    private WorkerDao workerDao;

    @Autowired
    private WorkerConverter workerConverter;

    @Override
    public void deleteWorker(Long userId) {
        workerDao.delete(userId);
        //设置key值
        String key="luban:worker:"+userId;
        //删除缓存
        deleteCache(key);
    }

    @Override
    public Long saveWorker(WorkerCreateParam workerCreateParam) {
        Worker worker = param2po(workerCreateParam);
        workerDao.save(worker);
        return worker.getId();
    }

    @Override
    public WorkerBO findWorker(Long userId) {

        WorkerBO workerBO;
        //设置key值
        String key="luban:worker:"+userId;
        if (!exists(key)){
            Worker worker=workerDao.selectWorkerByUserId(userId);
            workerBO = workerConverter.po2bo(worker);
            saveCache(key,workerBO);
        }else {
           workerBO=getCache(key);
        }
        return workerBO;

//        //查询缓存是否包含当前key
//        if (!redisTemplate.hasKey(key)){
//            //缓存不存在，查询数据库
//            Worker worker=workerDao.selectWorkerByUserId(userId);
//            WorkerBO workerBO = workerConverter.po2bo(worker);
//            //将查询结果保存到缓存中
//            redisTemplate.opsForValue().set(key,workerBO);
//            return workerBO;
//        }else {
//            //缓存中有，查询缓存将其返回
//            ValueOperations valueOperations = redisTemplate.opsForValue();
//            return (WorkerBO) valueOperations.get(key);
//        }

    }

    private Worker param2po(WorkerCreateParam workerCreateParam){

        Worker worker = new Worker();
        //调用workerDao 持久层  需要讲Param对象转换成PO对象
        if (workerCreateParam==null) {
            return worker;
        }

        BeanUtils.copyProperties(workerCreateParam,worker);
        //通用字段
        worker.setGmtCreate(System.currentTimeMillis());
        worker.setGmtModified(worker.getGmtCreate());
        worker.setCreateUserId(workerCreateParam.getUserId());
        LoginUser loginToken = SecurityContext.getLoginToken();
        worker.setCreateUserName(loginToken.getUserName());
        worker.setModifiedUserId(loginToken.getUserId());
        worker.setModifiedUserName(loginToken.getUserName());
        //师傅状态 0-锁定、1-正常
        worker.setStatus(StatusEnum.NORMAL.getValue());
        //审核状态 （0-驳回、1-已通过、2-未审核、3-未入住）
        worker.setAuditStatus(AuditStatusEnum.UNAUDITED.getValue());
        //平台账号入住状态（0-未认证、1-已认证）
        worker.setCertStatus(AccountStatusEnum.DISABLE.getValue());
        return worker;
    }
}
